package com.ruoyi.mateproduction.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;

import com.ruoyi.mateproduction.domain.ManageReview;
import com.ruoyi.mateproduction.domain.MateProductionRecord;
import com.ruoyi.mateproduction.domain.MateProductionRecordModel;
import com.ruoyi.mateproduction.domain.MateTransfer;
import com.ruoyi.mateproduction.domain.ReleaseReview;
import com.ruoyi.mateproduction.domain.UnqualifiedReview;
import com.ruoyi.mateproduction.mapper.MtProductionMapper;
import com.ruoyi.mateproduction.service.MateTransferService;
import com.ruoyi.mateproduction.service.MtProductionRecordService;
import com.ruoyi.mateproduction.service.MtProductionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/mateRecord")
public class MtProductionRecordController extends BaseController {

    @Autowired
    MtProductionRecordService mtProductionRecordService;

    @Autowired
    MtProductionService mtProductionService;

    @Autowired
    MateTransferService mateTransferService;

    @Autowired
    MtProductionMapper mtProductionMapper;

    /**
     * 保存批生产记录
     */
    @Log(title = "批生产记录", businessType = BusinessType.INSERT)
    @PostMapping("/addMateRecord")
    public AjaxResult addMateRecord(@RequestBody MateProductionRecord mateProductionRecord) {
        mateProductionRecord.setDeptId(SecurityUtils.getLoginUser().getCompanyId());
        int num = mtProductionRecordService.addMateRecord(mateProductionRecord);
        if (num > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }


    @PostMapping("/addManageReview")
    public AjaxResult addManageReview(@RequestBody ManageReview manageReview) {
        manageReview.setDeptId(SecurityUtils.getLoginUser().getCompanyId());
        int num = mtProductionMapper.addManageReview(manageReview);
        if (num > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    @GetMapping("/manageReviewList")
    @ResponseBody
    public TableDataInfo manageReviewList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                    @RequestParam(value = "endTime", required = false) String endTime) {
        ManageReview manageReview = new ManageReview();
        manageReview.setBeginTime(beginTime);
        manageReview.setEndTime(endTime);
        startPage();
        manageReview.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<ManageReview> list = mtProductionMapper.manageReviewList(manageReview);
        return getDataTable(list);
    }

    @GetMapping("/selectManageReview")
    public ManageReview selectManageReview(@RequestParam(value = "manageReviewId", required = false) Long manageReviewId) {
        ManageReview review = mtProductionMapper.selectManageReview(manageReviewId);
        return review;
    }

    /*
      查询已经审核通过的生产工序传递卡
       */
    @GetMapping("/auditMateTransfer")
    public Map<String, Object> auditMateTransfer() {
        Map<String, Object> resMap = new HashMap<>();
        List<MateTransfer> mateTransfers = mateTransferService.auditMateTransfer(SecurityUtils.getDeptId());
        resMap.put("result", mateTransfers);
        return resMap;
    }

    /*
     * 查询批生产记录
     */
    @GetMapping("/recordList")
    @ResponseBody
    public TableDataInfo recordList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                    @RequestParam(value = "endTime", required = false) String endTime) {
        MateProductionRecord mateProductionRecord = new MateProductionRecord();
        mateProductionRecord.setBeginTime(beginTime);
        mateProductionRecord.setEndTime(endTime);
        startPage();
        mateProductionRecord.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<MateProductionRecord> list = mtProductionRecordService.recordList(mateProductionRecord);
        return getDataTable(list);
    }


    /*
     * 查询成品放行单
     */
    @GetMapping("/releaseList")
    @ResponseBody
    public TableDataInfo releaseList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                     @RequestParam(value = "endTime", required = false) String endTime) {
        ReleaseReview releaseReview = new ReleaseReview();
        releaseReview.setBeginTime(beginTime);
        releaseReview.setEndTime(endTime);
        startPage();
        releaseReview.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<ReleaseReview> list = mtProductionMapper.releaseList(releaseReview);
        return getDataTable(list);
    }


    /*
   查询已经做完的批生产记录
    */
    @GetMapping("/selectReleaseReview")
    public ReleaseReview selectReleaseReview(@RequestParam(value = "releaseId", required = false) Long releaseId) {
        ReleaseReview releaseReview = mtProductionMapper.selectReleaseReview(releaseId);
        return releaseReview;
    }
    /*
   查询已经做完的批生产记录
    */
    @GetMapping("/selectRecord")
    public MateProductionRecord selectRecord(@RequestParam(value = "mtRecordId", required = false) Long mtRecordId) {
        MateProductionRecord mateProductionRecord = mtProductionRecordService.selectRecord(mtRecordId);
       /* List<MateProductionRecordModel> uniqueList = mateProductionRecord.getMateProductionRecordModels().stream()
                .collect(Collectors.toMap(MateProductionRecordModel::getProcessName, info -> info, (existing, replacement) -> existing))
                .values().stream()
                .collect(Collectors.toList());

        Collections.sort(uniqueList, (info1, info2) -> Math.toIntExact(info1.getMtRecordModelId() - info2.getMtRecordModelId()));
        mateProductionRecord.setMateProductionRecordModels(uniqueList);*/
        return mateProductionRecord;
    }

    /*
      编辑批生产记录
         */
    @PostMapping("/editRecord")
    @ResponseBody
    public AjaxResult editRecord(@RequestBody MateProductionRecord mateProductionRecord) {
        int num = mtProductionRecordService.editRecord(mateProductionRecord);
        if (num > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除生产记录
     */
    @Log(title = "删除生产记录", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{mtRecordId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("mtRecordId") Long mtRecordId) {
        mtProductionRecordService.remove(mtRecordId);
        return AjaxResult.success("删除成功");
    }

}
